﻿using System;
using Moven.Controls;
using Moven.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Support.V7.Widget;
using Android.Text;
using Android.Runtime;
using Android.Widget;
using Android.Content.Res;
using Java.Lang;
using Android.Views.InputMethods;
using Android.Views;
using System.ComponentModel;

[assembly:ExportRenderer(typeof(MaterialEntry), typeof(MaterialEntryRenderer))]
namespace Moven.Droid.Renderers
{
	public class MaterialEntryRenderer: ViewRenderer<Entry, AppCompatEditText>, ITextWatcher, INoCopySpan, IJavaObject, IDisposable, TextView.IOnEditorActionListener
	{
		//
		// Fields
		//
		private ColorStateList _hintTextColorDefault;

		private ColorStateList _textColorDefault;

		private AppCompatEditText _textView;

		//
		// Constructors
		//
		public MaterialEntryRenderer()
		{
			AutoPackage = false;
		}

		//
		// Methods
		//
		void ITextWatcher.AfterTextChanged(IEditable s)
		{
		}

		void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after)
		{
		}

		bool TextView.IOnEditorActionListener.OnEditorAction(TextView v, ImeAction actionId, KeyEvent e)
		{
			if (actionId == ImeAction.Done || (actionId == ImeAction.ImeNull && e.KeyCode == Keycode.Enter))
			{
				Control.ClearFocus();
				v.HideKeyboard(false);
				((IEntryController)Element).SendCompleted();
			}
			return true;
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
		{
			base.OnElementChanged(e);
			if (e.OldElement == null)
			{
				this._textView = new AppCompatEditText(Context);
				this._textView.ImeOptions = ImeAction.Done;
				this._textView.AddTextChangedListener(this);
				this._textView.SetOnEditorActionListener(this);
				SetNativeControl(this._textView);
			}
			this._textView.Hint = Element.Placeholder;
			this._textView.Text = Element.Text;
			this.UpdateInputType();
			this.UpdateColor();
			this.UpdateAlignment();
			this.UpdatePlaceholderColor();
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == Entry.PlaceholderProperty.PropertyName)
			{
				Control.Hint = Element.Placeholder;
			}
			else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName)
			{
				this.UpdateInputType();
			}
			else if (e.PropertyName == Entry.TextProperty.PropertyName)
			{
				if (Control.Text != Element.Text)
				{
					Control.Text = Element.Text;
					if (Control.IsFocused)
					{
						Control.SetSelection(Control.Text.Length);
						Control.ShowKeyboard();
					}
				}
			}
			else if (e.PropertyName == Entry.TextColorProperty.PropertyName)
			{
				this.UpdateColor();
			}
			else if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
			{
				this.UpdateInputType();
			}
			else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName)
			{
				this.UpdateAlignment();
			}
			else if (e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
			{
				this.UpdatePlaceholderColor();
			}
			base.OnElementPropertyChanged(sender, e);
		}

		void ITextWatcher.OnTextChanged(ICharSequence s, int start, int before, int count)
		{
			if (string.IsNullOrEmpty(Element.Text) && s.Length() == 0)
			{
				return;
			}
			((IElementController)Element).SetValueFromRenderer(Entry.TextProperty, s.ToString());
		}

		private void UpdateAlignment()
		{
			Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags();
		}

		private void UpdateColor()
		{
			if (!Element.TextColor.IsDefault())
			{
				if (this._textColorDefault == null)
				{
					this._textColorDefault = Control.TextColors;
				}
				Control.SetTextColor(Element.TextColor.ToAndroidPreserveDisabled(this._textColorDefault));
				return;
			}
			if (this._textColorDefault == null)
			{
				return;
			}
			Control.SetTextColor(this._textColorDefault);
		}

		private void UpdateInputType()
		{
			Entry element = Element;
			this._textView.InputType = element.Keyboard.ToInputType();
			if (element.IsPassword && (this._textView.InputType & InputTypes.ClassText) == InputTypes.ClassText)
			{
				this._textView.InputType = (this._textView.InputType | InputTypes.TextVariationPassword);
			}
			if (element.IsPassword && (this._textView.InputType & InputTypes.ClassNumber) == InputTypes.ClassNumber)
			{
				this._textView.InputType = (this._textView.InputType | InputTypes.DatetimeVariationDate);
			}
		}

		private void UpdatePlaceholderColor()
		{
			Color placeholderColor = Element.PlaceholderColor;
			if (!placeholderColor.IsDefault())
			{
				if (this._hintTextColorDefault == null)
				{
					this._hintTextColorDefault = Control.HintTextColors;
				}
				Control.SetHintTextColor(placeholderColor.ToAndroidPreserveDisabled(this._hintTextColorDefault));
				return;
			}
			if (this._hintTextColorDefault == null)
			{
				return;
			}
			Control.SetHintTextColor(this._hintTextColorDefault);
		}
	}
}
